package controller;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.CTPhieuXuat;
import model.PhieuXuat;
import model.KhachHang;

public class PhieuXuatController {
	// public static List<PhieuXuat> getCtpx(int idnv) {
	// Connection conn = DBConection.MYSQL;
	// if (conn == null)
	// return null;
	// try {
	// Statement stmt = conn.createStatement();
	// String sql = "SELECT * FROM hdphieuxuat WHERE idnv=" + idnv + "";
	// ResultSet rs = stmt.executeQuery(sql);
	// PhieuXuat px;
	// List<PhieuXuat> list = new ArrayList<>();
	// while (rs.next()) {
	// px = new PhieuXuat();
	// px.setMaHDPX(rs.getInt("maHDPX"));
	// px.setNgayXuat(rs.getDate("ngayXuat"));
	// KhachHangController d = new KhachHangController();
	// px.setKhachHang(d.getKHachHang(idnv));
	// CTPhieuXuatController ct = new CTPhieuXuatController();
	// List<CTPhieuXuat> ctpx = ct.getCtpx(rs.getInt("maHDPX"));
	// px.setCtpx(ctpx);
	// list.add(px);
	//
	// }
	// return list;
	// } catch (Exception e) {
	// e.printStackTrace();
	//
	// return null;
	// }
	// }

	public static List<PhieuXuat> getDSPhieuXuat() {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM hdxuat";
			ResultSet rs = stmt.executeQuery(sql);
			List<PhieuXuat> list = new ArrayList<>();
			PhieuXuat px;
			while (rs.next()) {
				px = new PhieuXuat();
				px.setMaPX(rs.getInt("id"));
				px.setIdNV(rs.getInt("idnv"));
				px.setIdKH(rs.getInt("idkh"));
				px.setNgayXuat(rs.getDate("ngaylap"));
				list.add(px);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static void insertPhieuXuat(PhieuXuat px) {

		try {
			Statement sta = DBConection.MYSQL.createStatement();
			String query = "INSERT INTO hdxuat (id,idnv,idkh,ngaylap) VALUES (";
			query += "'" + px.getMaPX() + "',";
			query += "'" + px.getIdNV() + "',";
			query += "'" + px.getIdKH() + "',";
			query += "'" + px.getNgayXuat();
			query += "')";
			System.out.println(query);
			sta.execute(query);
			return;

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		PhieuXuatController n = new PhieuXuatController();
		
		PhieuXuat p = new PhieuXuat();
		//p.setNgayXuat(new Date(2014,5,12));
		//n.insertPhieuXuat(p);
	}
}
